package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.RentSeekingDetailContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.RentSeekingDetail;
import com.kmproject.haifu.mvp.model.entity.beans.UserHomePageBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsCancelHouseRentSeekingCollection;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/11/2019 11:35
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class RentSeekingDetailPresenter extends BasePresenter<RentSeekingDetailContract.Model, RentSeekingDetailContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public RentSeekingDetailPresenter(RentSeekingDetailContract.Model model, RentSeekingDetailContract.View rootView) {
        super(model, rootView);
    }


    /**
     * 根据id获取详情
     *
     * @param seekingId
     */
    public void getDetail(String seekingId) {
        mModel.getDetail4Id(seekingId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<RentSeekingDetail>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<RentSeekingDetail> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getDetailSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.killMyself();
                        }
                    }
                });
    }

    /**
     * 删除
     *
     * @param seekingId
     */
    public void delete(String seekingId) {
        mModel.delete(seekingId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.deleteSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 收藏
     *
     * @param seekingId
     */
    public void collectionSeeking(String seekingId) {
        mModel.collectionSeeking(seekingId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.collectionSeekingSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 取消收藏求租
     *
     * @param reqParamsCancelHouseRentSeekingCollection
     */
    public void unCollectionSeeking(ReqParamsCancelHouseRentSeekingCollection reqParamsCancelHouseRentSeekingCollection) {
        mModel.unCollectionSeeking(AppUtil.entityToMap(reqParamsCancelHouseRentSeekingCollection)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.unCollectionSeekingSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    public void getUserHomePageInfo(String userId) {
        mModel.getUserHomePageInfo(userId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<UserHomePageBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<UserHomePageBean> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.showUserInfo(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
